package com.example.helper2;


import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.app.AlertDialog;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    // 获取登录按钮
    private TextView login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Fresco.initialize(this);
        // 获取登录组件
        login = findViewById(R.id.login);
        initLoginBtn();

        // 隐藏actionBar
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        // 获取tab栏
        mTabLayout = findViewById(R.id.tabs);
        // 获取viewpager容器
        mViewPager = findViewById(R.id.viewpager);
        // 设置容器
        this.setupViewPager(mViewPager);
        // 将viewpager和tablayout绑定
        mTabLayout.setupWithViewPager(mViewPager);
    }
    public void initLoginBtn(){
        login.setClickable(true);
        SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
        String user_name = sharedPreferences.getString("user_name",null);
        if (user_name == null || user_name.length() == 0){
            login.setText("登录");
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    fragment_login fl = new fragment_login();
                    FragmentManager fm = getSupportFragmentManager();
                    if (fm != null){
                        fl.show(fm,"login_dialog");
                    }
                }
            });
        }else{
            login.setText("欢迎，" + user_name);
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("提示");
                    builder.setMessage("确认注销？");
                    builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.remove("user_name");
                                editor.remove("user_word");
                                editor.remove("user_phone");
                                editor.remove("user_address");
                                editor.commit();
                                Toast.makeText(getApplication(),"注销成功！",Toast.LENGTH_SHORT).show();
                            }catch (Exception e){
                                Toast.makeText(getApplication(),"注销失败！",Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                            }
                            initLoginBtn();
                        }
                    });
                    builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
                    builder.show();

                }
            });
        }
    }

    private void setupViewPager(ViewPager viewPager){
        viewPagerAdapter adapter = new viewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new fragment_home(),"肺炎知识百科");
        adapter.addFragment(new fragment_time_line(),"时间线");
        adapter.addFragment(new fragment_new_latest(),"最新动态");
        adapter.addFragment(new fragment_defend_ways(),"防治措施");
        adapter.addFragment(new fragment_some_mistakes(),"相关辟谣");
        adapter.addFragment(new fragment_user_message(),"用户留言");
        viewPager.setAdapter(adapter);
    }

    class viewPagerAdapter extends FragmentPagerAdapter {
        // 各页面的Fragment
        private final List<Fragment> mFragmentList = new ArrayList<>();
        // 各页面的Title（标题）
        private final List<String> mFragmentTittleList = new ArrayList<>();
        public viewPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }
        @NonNull
        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }
        @Override
        public int getCount() {
            return mFragmentList.size();
        }
        // 添加一个页面
        public void addFragment (Fragment fragment, String tittle){
            mFragmentList.add(fragment);
            mFragmentTittleList.add(tittle);
        }
        // 重写方法，设置每个ViewPager的标题。ViewPager最终会填入到Tabs中
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTittleList.get(position);
        }
    }

}
